<?php

	/**
	 * @package		CoreProject
	 * @subpackage	Mime
	 *
	 * cpr_Mime_Mail
	 * cpr_Mime_Mails
	 * cpr_Mime_GlobalCharset
	 *
	 */

	class cpr_Mime_Mail
	{
		/**
		 * @var	cpr_Mime_GlobalCharset
		 */
		private $_global_charset;

		/**
		 * @var cpr_Mime_MimePart
		 */
		private $_mime;

		/**
		 * @var cpr_Mime_Headers
		 */
		private $_headers;

		/**
		 * @var cpr_Mime_Attachments
		 */
		private $_attachments;

		/**
		 * @var cpr_Mime_TextBody
		 */
		private $_text;

		/**
		 * @var	string
		 */
		private $_raw;

		/**
		 * @var	int
		 */
		private $_size;

		/**
		 * @param	string	$_defaultCharset
		 * @param	string	$_rawData
		 * @param	bool	$_onlyHeader = false
		 * @param	bool	$_keepRawBody = false
		 * @return	cpr_Mime_MimePart
		 */
		function cpr_Mime_Mail($_defaultCharset, &$_rawData, $_onlyHeader = false, $_keepRawBody = false)
		{
			$this->_global_charset = new cpr_Mime_GlobalCharset($_defaultCharset);
			if ($_onlyHeader)
			{
				$this->_headers = new cpr_Mime_Headers($this->_global_charset, $_rawData);
			}
			else
			{
				$this->_size = strlen($_rawData);
				if ($_keepRawBody)
				{
					$this->_raw =& $_rawData;
				}
				$this->_mime = new cpr_Mime_MimePart($this->_global_charset, $_rawData);
				$this->_headers =& $this->_mime->GetHeaders();
				$this->_attachments = new cpr_Mime_Attachments($this->_global_charset, $this->_mime);
				$this->_text = new cpr_Mime_TextBody($this->_global_charset, $this->_mime);
			}
		}

		/**
		 * @return	cpr_Mime_Attachments
		 */
		function &GetAttachments()
		{
			if ($this->_attachments)
			{
				return $this->_attachments;
			}
			$null = null;
			$_attachemnts = new cpr_Mime_Attachments($this->_global_charset, $null);
			return $_attachemnts;
		}

		/**
		 * @return	int
		 */
		function GetAttachmentsCount()
		{
			$_attch =& $this->GetAttachments();
			return $_attch->Count();
		}

		/**
		 * @return	bool
		 */
		function HasAttachments()
		{
			if ($this->_mime)
			{
				$_attch =& $this->GetAttachments();
				return $_attch->Count() > 0;
			}
			else
			{
				return $this->_headers->IsAttachmentMailMessage();
			}
		}

		/**
		 * @return	string
		 */
		function GetSubject()
		{
			return cpr_Core_Utils::ClearNRT($this->_headers->GetHeaderDecodedValueByName(CPR_MIME_SUBJECT));
		}

		/**
		 * @return	string
		 */
		function GetDateStringHeaderValue()
		{
			return cpr_Core_Utils::ClearNRT($this->_headers->GetHeaderValueByName(CPR_MIME_DATE));
		}

		/**
		 * @return	string
		 */
		function GetDateStringReceivedHeaderValue()
		{
			$date = '';
			$receivedValues = $this->_headers->GetHeadersValuesByName(CPR_MIME_RECEIVED);
			foreach ($receivedValues as $received)
			{
				if (false !== strpos($received, ';'))
				{
					$receivedArr = explode(';', $received);
					if (preg_match('/ [\d]{4} [\d]{2}:[\d]{2}:[\d]{2} /', $receivedArr[count($receivedArr)-1]))
					{
						$date = trim($receivedArr[count($receivedArr)-1]);
						break;
					}
				}
			}
			
			return $date;
		}

		/**
		 * @return	string
		 */
		public function GetDateString()
		{
			$dateReceived = $this->GetDateStringReceivedHeaderValue();
			if ('' === $dateReceived)
			{
				$dateReceived = $this->GetDateStringHeaderValue();
			}
			return $dateReceived;
		}

		/**
		 * @return	string
		 */
		function GetPlainText()
		{
			return $this->_text->GetPlain();
		}

		/**
		 * @return	string
		 */
		function GetHtmlText()
		{
			return $this->_text->GetHtml();
		}

		/**
		 * @returnint
		 */
		function Size()
		{
			return $this->_size;
		}

		/**
		 * @return	string
		 */
		function GetHtmlOrPlain()
		{
			$_type = $this->_text->GetType();
			if (($_type & cpr_Mime_TextBody::TYPE_HTML) == cpr_Mime_TextBody::TYPE_HTML)
			{
				return $this->GetHtmlText();
			}
			else
			{
				return $this->GetPlainText();
			}
		}

		/**
		 * @return	int
		 */
		function GetTextType()
		{
			return $this->_text->GetType();
		}
	}

	class cpr_Mime_Mails extends cpr_Core_Collection {}

	class cpr_Mime_GlobalCharset
	{
		/**
		 * @var	string
		 */
		private $_out_charset;

		/**
		 * @var	string
		 */
		private $_def_charset;

		/**
		 * @var	string
		 */
		private $_mail_charset = null;

		/**
		 * @param	string		$def_charset
		 * @return	cpr_Mime_GlobalCharset
		 */
		function  cpr_Mime_GlobalCharset($def_charset)
		{
			$this->_def_charset = $def_charset;
			$this->_out_charset = $def_charset;
		}

		/**
		 * @return	string
		 */
		public function GetMailCharset()
		{
			if (null === $this->_mail_charset)
			{
				return $this->_def_charset;
			}
			return $this->_mail_charset;
		}

		/**
		 * @param	string	$_charset
		 */
		public function SetMailCharset($_charset)
		{
			if (null === $this->_mail_charset)
			{
				$this->_mail_charset = strtolower($_charset);
			}
		}

		/**
		 * @return	string
		 */
		public function GetOutCharset()
		{
			return $this->_out_charset;
		}

		/**
		 * @return	string
		 */
		public function GetDbCharset()
		{
			return CPR_MIME_CHARSET_UTF8;
		}
	}